package cn.neusoft.query;

import cn.neusoft.pojo.Business;
import cn.neusoft.pojo.Food;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.Data;

@Data
public class FoodQuery extends BaseQuery{

    private String foodName; //餐品名称搜索条件
    private Integer businessId; //商家编号

//    @Override
//    public Wrapper getQueryWrapper() {
//        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
//        if (StringUtils.isNotEmpty(foodName)){
//            queryWrapper.like("foodName",foodName);
//        }
//        if (businessId!=null && businessId!=0){
//            queryWrapper.eq("f.businessId",businessId);
//        }
//        return queryWrapper;
//    }


    /**
     * select f.foodId, f.foodName, f.foodExplain, foodImg, foodPrice, f.businessId, f.remarks ,
     *                b.businessName as 'business.businessName'
     *         from t_food f left join t_business b on f.businessId=b.businessId
     *         <if test="ew.sqlSegment!=null and ew.sqlSegment!=''">
     *             where ${ew.sqlSegment}
     *         </if>
     *         order by foodId desc
     * @return
     */

    @Override
    public Wrapper getQueryWrapper() {
        MPJLambdaWrapper<Food> queryWrapper = new MPJLambdaWrapper<Food>()
                .setAlias("f")
                .selectAll(Food.class,"f")
                .selectAs(Business::getBusinessName,"'business.businessName'")
                .selectAs(Business::getBusinessAddress, "'business.businessAddress'")
                .leftJoin(Business.class,"b",Business::getBusinessId,Food::getBusinessId)
                .orderByDesc("foodId");
        //搜索条件
        if (StringUtils.isNotEmpty(foodName)){
            queryWrapper.like("foodName",foodName);
        }
        if (businessId!=null && businessId!=0){
            queryWrapper.eq("f.businessId",businessId);
        }
        return queryWrapper;
    }


}
